/* eslint-disable */
import _ from 'lodash'

export default {
    state: {
        userName: null,
        name: null,
        token: null,
        friends: []
    },
    actions: {
        createPeer ({commit,getters},{friendName,stream}) {
            var peer = new RTCPeerConnection(getters.RTCConfig);
            peer.addStream(getters.LocalVideoStream);
            peer.createOffer
        },
        JoinRoom ({commit},{socket,roomName,userName}){
            
        }
    },
    mutations: {
        saveLoginfo(state,{userName,name,token,friends}){
            state.userName = userName;
            state.name = userName;
            state.token = token;
            state.friends = friends;
            console.log(friends);
        },
        friendLogout(state,friend){
            //_.remove(state.friends,_.iteratee({name:friend.name}));
            var i = _.findIndex(state.friends,{name:friend.name});
            i >= 0 && state.friends.splice(i,1);
        },
        friendLogin(state,friend){
            state.friends.push(friend);
        },
        friendOffer(state,{name,ping,pong}){
            var friend = _.find(state.friends,{name});
            friend && pong && (friend.pong = pong);
            friend && ping && (friend.ping = ping);
        },
        friendAnswer(state,{name,sdp}){
            var friend = _.find(state.friends,{name});
            friend && friend.ping.setRemoteDescription(sdp);
        },
        friendICE(state,{name,type,ice}){
            var friend = _.find(state.friends,{name});
            var candidate = new RTCIceCandidate({sdpMLineIndex:ice.sdpMLineIndex, sdpMid:ice.sdpMid, candidate:ice.candidate});
            console.log(candidate);
            friend && friend[type].addIceCandidate(candidate);
        }
    }
}